package com.yang.dao;

import com.yang.entity.Orders;

import java.sql.*;
import java.util.*;

public class OrdersDao {
    //查询全部
    public List<Orders> queryOrders() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Orders");
            rs = ps.executeQuery();
            List<Orders> list = new ArrayList<>();
            while (rs.next()) {
                Orders obj = new Orders();
                obj.setId(rs.getString("id"));
                obj.setUsername(rs.getString("username"));
                obj.setAddress(rs.getString("address"));
                obj.setCreationtime(rs.getDate("creationtime"));
                obj.setTel(rs.getString("tel"));
                obj.setTotalamount(rs.getObject("totalamount"));
                obj.setStatus(rs.getInt("status"));
                obj.setUserid(rs.getInt("userid"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Orders queryOrdersById(String id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Orders where id=?");
            ps.setString (1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Orders obj = new Orders();
                obj.setId(rs.getString("id"));
                obj.setUsername(rs.getString("username"));
                obj.setAddress(rs.getString("address"));
                obj.setCreationtime(rs.getDate("creationtime"));
                obj.setTel(rs.getString("tel"));
                obj.setTotalamount(rs.getObject("totalamount"));
                obj.setStatus(rs.getInt("status"));
                obj.setUserid(rs.getInt("userid"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveOrders(Orders obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into orders(userName, address, creationTime, tel, totalAmount, status, userId) values (?, ?, ?, ?, ?, ?, ?)");
            ps.setString(1, obj.getUsername());
            ps.setString(2, obj.getAddress());
            ps.setDate(3, obj.getCreationtime());
            ps.setString(4, obj.getTel());
            ps.setObject(5, obj.getTotalamount());
            ps.setInt(6, obj.getStatus());
            ps.setInt(7, obj.getUserid());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteOrdersById(String id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Orders where id=?");
            ps.setString (1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateOrdersById(Orders obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update orders set userName=?, address=?, creationTime=?, tel=?, totalAmount=?, status=?, userId=? where id=?");
            ps.setString(1, obj.getUsername());
            ps.setString(2, obj.getAddress());
            ps.setDate(3, obj.getCreationtime());
            ps.setString(4, obj.getTel());
            ps.setObject(5, obj.getTotalamount());
            ps.setInt(6, obj.getStatus());
            ps.setInt(7, obj.getUserid());
            ps.setString(8, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
